<?php

namespace App\Http\Middleware;

use App\Http\Admin\Logics\LoginLogic;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\View;
use Symfony\Component\HttpFoundation\Response;

class Authenticate
{
    /**
     * 处理传入请求。
     *
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
     */
    public function handle(Request $request, Closure $next): Response
    {
        // 判断是否登录, 未登录跳转登录页面
        if(!LoginLogic::check()) {
            return redirect(route('login'));
        }

        View::share('username', LoginLogic::getUsername());
        return $next($request);
    }
}
